package com.ruoyi.biz.service.impl;

import java.util.List;

import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.biz.mapper.BizSubmitWorkMapper;
import com.ruoyi.biz.domain.BizSubmitWork;
import com.ruoyi.biz.service.IBizSubmitWorkService;

import javax.annotation.Resource;

/**
 * 作业提交Service业务层处理
 * 
 * @author lhb
 * @date 2025-02-24
 */
@Service
public class BizSubmitWorkServiceImpl implements IBizSubmitWorkService 
{
    @Resource
    private BizSubmitWorkMapper bizSubmitWorkMapper;

    /**
     * 查询作业提交
     * 
     * @param id 作业提交主键
     * @return 作业提交
     */
    @Override
    public BizSubmitWork selectBizSubmitWorkById(Long id)
    {
        return bizSubmitWorkMapper.selectBizSubmitWorkById(id);
    }

    /**
     * 查询作业提交列表
     * 
     * @param bizSubmitWork 作业提交
     * @return 作业提交
     */
    @Override
    public List<BizSubmitWork> selectBizSubmitWorkList(BizSubmitWork bizSubmitWork)
    {
        return bizSubmitWorkMapper.selectBizSubmitWorkList(bizSubmitWork);
    }

    /**
     * 新增作业提交
     * 
     * @param bizSubmitWork 作业提交
     * @return 结果
     */
    @Override
    public int insertBizSubmitWork(BizSubmitWork bizSubmitWork)
    {
        bizSubmitWork.setCreateTime(DateUtils.getNowDate());
        bizSubmitWork.setState(0L);

        // 检测是否重复提交作业 根据学生id和作业id查询数据库是否已经存在数据
        Long studentId = bizSubmitWork.getStudentId();
        Long workId = bizSubmitWork.getWorkId();
        BizSubmitWork bizSubmitWork1 = bizSubmitWorkMapper.selectByWorkIdAndStudentId(workId, studentId);
        if(ObjectUtils.isNotEmpty(bizSubmitWork1)){
            // 存在则修改
            bizSubmitWork.setId(bizSubmitWork1.getId());
            return updateBizSubmitWork(bizSubmitWork);
        }

        return bizSubmitWorkMapper.insertBizSubmitWork(bizSubmitWork);
    }

    /**
     * 修改作业提交
     * 
     * @param bizSubmitWork 作业提交
     * @return 结果
     */
    @Override
    public int updateBizSubmitWork(BizSubmitWork bizSubmitWork)
    {
        bizSubmitWork.setUpdateTime(DateUtils.getNowDate());
        return bizSubmitWorkMapper.updateBizSubmitWork(bizSubmitWork);
    }

    /**
     * 批量删除作业提交
     * 
     * @param ids 需要删除的作业提交主键
     * @return 结果
     */
    @Override
    public int deleteBizSubmitWorkByIds(Long[] ids)
    {
        return bizSubmitWorkMapper.deleteBizSubmitWorkByIds(ids);
    }

    /**
     * 删除作业提交信息
     * 
     * @param id 作业提交主键
     * @return 结果
     */
    @Override
    public int deleteBizSubmitWorkById(Long id)
    {
        return bizSubmitWorkMapper.deleteBizSubmitWorkById(id);
    }
}
